
var container, scene, camera, renderer, controls, stats,light;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();

var SCREEN_WIDTH,SCREEN_HEIGHT;

function initThree(){
     initScene();
     initCamera();
     initRenderer();
     initContainer();
     initEvents();
     initControls();
     initStats();
     initLight();
 }

function initRenderer()
{
    if ( Detector.webgl )
        renderer = new THREE.WebGLRenderer( {antialias:false} );
    else
        renderer = new THREE.CanvasRenderer();
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
}

function initStats()
{
    stats = new Stats();
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.bottom = '0px';
    stats.domElement.style.zIndex = 100;
    container.appendChild( stats.domElement );
}

function initCamera() {
    SCREEN_WIDTH = window.innerWidth;
    SCREEN_HEIGHT = window.innerHeight;
    camera = new THREE.PerspectiveCamera( 45, SCREEN_WIDTH / SCREEN_HEIGHT, 0.1, 1000000);
    scene.add(camera);
    camera.position.set(0,0,4000);
    camera.lookAt(scene.position);
}

function initScene() {
    scene = new THREE.Scene();
}

function initLight()
{
    light = new THREE.PointLight(0xffffff,2,40000,0.3);
    light.position.set(offsetX,0,0);
    scene.add(light);
}

function initContainer(){
    container = document.getElementById( 'ThreeJS' );
    container.appendChild( renderer.domElement );
}

function initEvents(){
    THREEx.WindowResize(renderer, camera);
    THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
}

function initControls()
{
    controls = new THREE.OrbitControls( camera, renderer.domElement );
    controls.autoRotate = true;
    controls.autoRotateSpeed=50;
    controls.enableDamping = true;
    controls.dampingFactor = 0.5;
    controls.update();
}

